﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Drawing;
using System.Windows.Forms;
using GatewayInspect.Common;
using GatewayInspect.Model;

namespace GatewayInspect.Modules
{
    public partial class MGCYModule : BaseModule
    {
        private IDCradReader reader = null;

        public MGCYModule()
        {
            InitializeComponent();
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();

            this.tsBtnImportExcel.Visible = false;
            this.tsBtnDelete.Visible = false;
            this.tsBtnDownTemplate.Visible = false;
            this.tsBtnRefresh.Visible = false;
            this.tsBtnSave.Visible = false;
            this.toolStripSeparator2.Visible = false;
            this.toolStripSeparator3.Visible = false;
        }

        protected override void OnModuleDestroyed()
        {
            base.OnModuleDestroyed();
            if (reader != null)
            {
                reader.AfterReadInfo -= new IDCradReader.AfterReadInfoHandler(reader_AfterReadInfo);
                reader.CheckHandler();//检查是否还有其他对象读卡
            }
        }

        protected override void OnInit()
        {
            base.OnInit();
            SetTruckInfo(null);
            BeginRead();//启动身份证读卡设备开始读卡 
        }

        private void BeginRead()
        {
            if (reader == null)
            {
                reader = IDCradReader.GetInstance();
                reader.AfterReadInfo += new IDCradReader.AfterReadInfoHandler(reader_AfterReadInfo);
            }
            reader.BeginRead();
        }

        private void reader_AfterReadInfo(CardInfo info)
        {
            if (IsActive)
            {
                SetCardInfo(info);
                if (!string.IsNullOrEmpty(info.CardNo))
                {
                    this.tbxDriverID.Text = info.CardNo;
                    TruckInfo_Readonly truckInfo = (TruckInfo_Readonly)this.propertyTruck.SelectedObject;
                    truckInfo.DRIVERID = info.CardNo;
                    truckInfo.DRIVERNAME = info.Name;
                    SetTruckInfo(truckInfo);
                }
            }
        }

        //private void GetTruckInfo(DataTable dtTruck)
        //{
        //    if (dtTruck == null || dtTruck.Rows.Count <= 0)
        //    {
        //        MessageBox.Show("无符合条件查询结果！");
        //        return;
        //    }
        //    TruckInfo_Readonly info = null;
        //    if (dtTruck.Rows.Count > 1)
        //    {
        //        using (SelectTruckForm form = new SelectTruckForm(dtTruck))
        //        {
        //            if (form.ShowDialog() == DialogResult.OK)
        //            {
        //                info = TruckInfo_Readonly.GetInstanceFromDB(form.SelectedRow);
        //            }
        //        }
        //    }
        //    else
        //    {
        //        info = TruckInfo_Readonly.GetInstanceFromDB(dtTruck.Rows[0]);
        //    }
        //    SetTruckInfo(info);
        //}

        private void SetCardInfo(CardInfo info)
        {
            if (info != null)
            {
                txtCardID.Text = info.CardNo;
                txtName.Text = info.Name;
                txtSex.Text = info.Sex;
                txtBirthday.Text = info.Birthday;
                txtStartDate.Text = info.StartDate;
                txtEndDate.Text = info.EndDate;
                txtNation.Text = info.Nation;
                txtAddress.Text = info.Address;

                System.IO.MemoryStream objMSPhoto = new System.IO.MemoryStream(info.ArrPhotoByte, 0, info.ArrPhotoByte.Length);
                Image objImage = Image.FromStream(objMSPhoto);
                this.pnlPhoto.BackgroundImage = objImage;
            }
            else
            {
                txtCardID.Text = string.Empty;
                txtName.Text = string.Empty;
                txtSex.Text = string.Empty;
                txtBirthday.Text = string.Empty;
                txtStartDate.Text = string.Empty;
                txtEndDate.Text = string.Empty;
                txtNation.Text = string.Empty;
                txtAddress.Text = string.Empty;

                this.pnlPhoto.BackgroundImage = null;
            }
        }

        private void SetTruckInfo(TruckInfo_Readonly info)
        {
            SetTruckProperty(info);
            SetBlackHistory(info);
        }

        private void SetTruckProperty(TruckInfo_Readonly info)
        {
            if (info == null)
            {
                info = new TruckInfo_Readonly();
            }
            else
            {
                if (!string.IsNullOrEmpty(info.TRUCKNO))
                    info.LastInspectTime = GetLastInspectTime(info.TRUCKNO);//上次查验日期
                if (!string.IsNullOrEmpty(info.DRIVERID))
                    CheckSafetyInform(info.DRIVERID);//是否安全告知
            }
            this.propertyTruck.SelectedObject = info;
            this.propertyTruck.ExpandAllGridItems();
        }

        private void SetBlackHistory(TruckInfo_Readonly info)
        {
            this.cdgvBlackHistory.DataSource = null;
            if (info == null || string.IsNullOrEmpty(info.DRIVERID) || string.IsNullOrEmpty(info.TRUCKNO))
                return;
            DataTable dtBlackHistory = GetExpiredZJinfo(info);
            DataTable dtTemp = GetBlacklist(info.TRUCKNO, info.DRIVERID);
            if (dtTemp != null && dtTemp.Rows.Count > 0)
            {
                MessageBox.Show("该车辆或该驾驶员已加入黑名单！", "黑名单提醒");
                dtBlackHistory.Merge(dtTemp);
            }
            dtTemp = GetNotPassedCYJL(info.TRUCKNO, info.DRIVERID);
            if (dtTemp != null && dtTemp.Rows.Count > 0)
            {
                txtWTGCS.Text = dtTemp.Rows.Count.ToString();//查验未通过次数
                dtBlackHistory.Merge(dtTemp);
            }
            else
            {
                txtWTGCS.Text = "0";//查验未通过次数
            }
            this.cdgvBlackHistory.DataSource = dtBlackHistory;
        }

        private void CheckSafetyInform(string sDriverID)
        {
            string strSQL = "select ID from T_SAFETY_INFORM where ORGCODE = '{0}' and DRIVERID = '{1}' and TRUNC(informtime) = TRUNC(sysdate)";
            DataTable dtInform = DataAccess.ExecuteDataTable(string.Format(strSQL, Session.CurrentOrgCode, sDriverID));
            if (dtInform != null && dtInform.Rows.Count > 0)
            {
                chbAQGZ.Checked = true;
            }
            else
            {
                chbAQGZ.Checked = false;
            }
        }

        private DateTime GetLastInspectTime(string sTruckNo)
        {
            string strSQL = "select max(INSPECTTIME) from T_TRUCK_GATEINSPECT where ORGCODE = '{0}' and TRUCKNO = '{1}' and PASSED = '1'";
            object oValue = DataAccess.ExecuteScalar(string.Format(strSQL, Session.CurrentOrgCode, sTruckNo));
            if (oValue != null && oValue != DBNull.Value)
            {
                try
                {
                    return Convert.ToDateTime(oValue);
                }
                catch
                {
                    return DateTime.MinValue;
                }
            }
            return DateTime.MinValue;
        }

        #region 获取车辆存在问题列表

        /// <summary>
        /// 获取过期证件信息
        /// </summary>
        /// <param name="info"></param>
        private DataTable GetExpiredZJinfo(TruckInfo_Readonly info)
        {
            DataTable dtResult = GetBlackHistoryTable();
            CheckYXQ("灭火器", info.MHQ_YXQ, dtResult);
            CheckYXQ("行驶证-车头-检验有效期", info.XSZ_CT_YXQ, dtResult);
            CheckYXQ("行驶证-车挂-检验有效期", info.XSZ_CG_YXQ, dtResult);
            CheckYXQ("道路运输证-车头-车辆审验及技术等级记录", info.YSZ_CG_SYJL, dtResult);
            CheckYXQ("道路运输证-车挂-车辆审验及技术等级记录", info.YSZ_CG_SYJL, dtResult);
            CheckYXQ("罐体定期检验报告", info.JYBG_YXQ, dtResult);
            return dtResult;
        }

        private DataTable GetBlackHistoryTable()
        {
            DataTable dtResult = new DataTable();
            dtResult.Columns.Add("来源", typeof(string));
            dtResult.Columns.Add("日期", typeof(DateTime));
            dtResult.Columns.Add("问题", typeof(string));
            dtResult.Columns.Add("处理办法", typeof(string));
            dtResult.Columns.Add("车牌号", typeof(string));
            dtResult.Columns.Add("司机身份证号", typeof(string));
            dtResult.Columns.Add("司机姓名", typeof(string));
            dtResult.Columns.Add("ID", typeof(string));
            return dtResult;
        }

        private void CheckYXQ(string name, DateTime _yxq, DataTable result)
        {
            if (_yxq != null && _yxq <= DateTime.Now)
            {
                DataRow row = result.NewRow();
                row["来源"] = "证件有效期检查";
                //row["日期"] = _yxq;
                row["问题"] = name + " 过期！";
                result.Rows.Add(row);
            }
        }

        /// <summary>
        /// 获取黑名单信息(各个库区通用)
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        private DataTable GetBlacklist(string truckno, string driverid)
        {
            string strSql = "select '黑名单' as 来源, ADDTIME as 日期, REASON as 问题, MEASURE as 处理办法, TRUCKNO as 车牌号, DRIVERID as 司机身份证号, DRIVERNAME as 司机姓名, ID" +
                " from T_TRUCK_BLACKLIST where ISDEL = '0' and (TRUCKNO = '" + truckno + "' or DRIVERID = '" + driverid + "') order by ADDTIME desc";
            return DataAccess.ExecuteDataTable(strSql);
        }

        /// <summary>
        /// 获取未通过查验记录信息（当前库区内有效）
        /// </summary>
        /// <returns></returns>
        private DataTable GetNotPassedCYJL(string truckno, string driverid)
        {
            string strSql = "select '查验记录' as 来源, INSPECTTIME as 日期, PROBLEMS as 问题, MEASURES as 处理办法, TRUCKNO as 车牌号, DRIVERID as 司机身份证号, DRIVERNAME as 司机姓名" +
                " from T_TRUCK_GATEINSPECT where ORGCODE = '" + Session.CurrentOrgCode + "' and PASSED = '0' and (TRUCKNO = '" + truckno + "' or DRIVERID = '" + driverid + "') order by INSPECTTIME desc";
            return DataAccess.ExecuteDataTable(strSql);
        }

        #endregion

        private void btnQueryDriver_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tbxDriverID.Text))
            {
                MessageBox.Show("身份证号不能为空！");
                return;
            }
            string strSql = "select DRIVERID,DRIVERNAME from T_SAFETY_INFORM where ORGCODE = '" + Session.CurrentOrgCode + "' and DRIVERID = '" + tbxDriverID.Text + "' and rownum <=1";
            DataTable dt = DataAccess.ExecuteDataTable(strSql);
            if (dt != null && dt.Rows.Count > 0)
            {
                TruckInfo_Readonly truckInfo = (TruckInfo_Readonly)this.propertyTruck.SelectedObject;
                truckInfo.DRIVERID = dt.Rows[0]["DRIVERID"].ToString();
                truckInfo.DRIVERNAME = dt.Rows[0]["DRIVERNAME"].ToString();
                SetTruckInfo(truckInfo);
                SetCardInfo(null);
            }
            else
            {
                MessageBox.Show("未查询到司机信息！");
            }
        }

        private void btnQueryTruck_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tbxTruckNo.Text))
            {
                MessageBox.Show("车牌号不能为空！");
                return;
            }
            string strSql = "select * from T_TRUCK_CERT_INFO where TRUCKNO = '" + tbxTruckNo.Text + "'";
            DataTable dt = DataAccess.ExecuteDataTable(strSql);
            if (dt != null && dt.Rows.Count > 0)
            {
                TruckInfo_Readonly info = TruckInfo_Readonly.GetInstanceFromDB(dt.Rows[0]);
                TruckInfo_Readonly oldInfo = (TruckInfo_Readonly)this.propertyTruck.SelectedObject;
                info.DRIVERID = oldInfo.DRIVERID;
                info.DRIVERNAME = oldInfo.DRIVERNAME;
                SetTruckInfo(info);
                //SetCardInfo(null);
                SelectInformedDriver();
            }
            else
            {
                MessageBox.Show("未查询到车辆信息！");
            }
        }

        private void SelectInformedDriver()
        {
            string strSql = "select distinct DRIVERID,DRIVERNAME from T_SAFETY_INFORM a where a.ORGCODE = '" + Session.CurrentOrgCode + "' " +
                "and TRUNC(a.informtime) = TRUNC(sysdate) and not exists(" +
                "select id from T_TRUCK_GATEINSPECT b where b.ORGCODE='" + Session.CurrentOrgCode + "' and b.Driverid = a.driverid and b.inspecttime>a.informtime)";
            DataTable dt = DataAccess.ExecuteDataTable(strSql);
            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt.Rows.Count == 1)
                {
                    TruckInfo_Readonly info = (TruckInfo_Readonly)this.propertyTruck.SelectedObject;
                    info.DRIVERID = dt.Rows[0]["DRIVERID"].ToString();
                    info.DRIVERNAME = dt.Rows[0]["DRIVERNAME"].ToString();
                    SetTruckInfo(info);
                }
                else
                {
                    using (SelectDriverForm form = new SelectDriverForm(dt))
                    {
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            TruckInfo_Readonly info = (TruckInfo_Readonly)this.propertyTruck.SelectedObject;
                            info.DRIVERID = form.DriverID;
                            info.DRIVERNAME = form.DriverName;
                            SetTruckInfo(info);
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("无已安全告知且未查验过的司机！");
            }
        }

        /// <summary>
        /// 查验通过
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFK_Click(object sender, EventArgs e)
        {
            TruckInfo_Readonly info = (TruckInfo_Readonly)this.propertyTruck.SelectedObject;
            if (info == null)
                return;
            if (string.IsNullOrEmpty(info.DRIVERID) || string.IsNullOrEmpty(info.TRUCKNO))
            {
                MessageBox.Show("车牌号，司机身份证号不能为空！");
                return;
            }

            GenerateCYJL(info, true);//生成查验记录

            DataTable dtFK = DataAccess.ExecuteDataTable("select CARDID,HD_WEIGHT from RFID_READ where CHEHAO='" + info.TRUCKNO + "' and CARDSTATE='0'");
            if (dtFK != null && dtFK.Rows.Count > 0)//已发卡
            {
                MessageBox.Show("车辆查验通过！");
                //if (CheckReSendCard(dtFK.Rows[0]["CARDID"].ToString()))
                //{
                //    ShowFKModule(info.TRUCKNO, info.DRIVERID, dtFK.Rows[0]["HD_WEIGHT"].ToString());
                //}
            }
            else//未发卡
            {
                ShowFKModule(info.TRUCKNO, info.DRIVERID, info.HD_WEIGHT.ToString());
            }
            Clear();
        }

        //private bool CheckReSendCard(string _cardNo)
        //{
        //    using (MGCY_YFKForm form = new MGCY_YFKForm(_cardNo))
        //    {
        //        if (form.ShowDialog() == DialogResult.Yes)//重新发卡
        //        {
        //            //先废卡
        //            DataAccess.ExecuteNonQuery("update RFID_READ set CARDSTATE='1' where ORGCODE='" + Session.CurrentOrgCode + "' and CARDSTATE = '0' and CARDID = '" + _cardNo + "'");
        //            return true;
        //        }
        //        return false;
        //    }
        //}

        /// <summary>
        /// 弹出发卡模块
        /// </summary>
        /// <param name="_truckNo"></param>
        /// <param name="_driverId"></param>
        private void ShowFKModule(string _truckNo, string _driverId, string _weight = "0")
        {
            using (PopupForm form = new PopupForm())
            {
                FKModule oModule = new FKModule();
                oModule.Parent = form;
                oModule.Dock = DockStyle.Fill;
                oModule.tsMain.Visible = false;
                oModule.TruckNo = _truckNo;
                oModule.DriverID = _driverId;
                oModule.HD_Weight = _weight;

                form.Text = "发卡";
                if (form.ShowDialog() == DialogResult.OK)
                {
                    MessageBox.Show("发卡成功！");
                }
                else
                {
                    MessageBox.Show("已查验通过未发卡！");
                }
            }
        }

        /// <summary>
        /// 查验未通过
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFail_Click(object sender, EventArgs e)
        {
            TruckInfo_Readonly info = (TruckInfo_Readonly)this.propertyTruck.SelectedObject;
            if (info == null)
                return;
            if (string.IsNullOrEmpty(info.ID) || string.IsNullOrEmpty(info.TRUCKNO))
            {
                MessageBox.Show("车牌号，司机身份证号不能为空！");
                return;
            }
            using (MGCY_NotPassedForm form = new MGCY_NotPassedForm(info.TRUCKNO, info.DRIVERID))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    GenerateCYJL(info, false, form.Problems, form.Measures);//生成查验记录
                    if (form.AddToBlackList)//加入黑名单
                    {
                        AddToBlacklist(info, form.Problems, form.Measures, form.NeedToDate, form.ToDate);
                    }
                    Clear();
                }
            }
        }

        /// <summary>
        /// 生成查验记录
        /// </summary>
        /// <param name="info"></param>
        /// <param name="passed"></param>
        /// <param name="problems"></param>
        /// <param name="measures"></param>
        private void GenerateCYJL(TruckInfo_Readonly info, bool passed, string problems = "", string measures = "")
        {
            string strSQL = "insert into T_TRUCK_GATEINSPECT(ID,TRUCKNO,DRIVERID,DRIVERNAME,PASSED,SFZ,JSZ,XSZ,DLYSZ,DLYSCYZGZ,GTJYBG,INSPECTMAN,INSPECTTIME,BZ,PROBLEMS,MEASURES,SFAQGZ,MHQ,ORGCODE)"
                + " values(:ID,:TRUCKNO,:DRIVERID,:DRIVERNAME,:PASSED,:SFZ,:JSZ,:XSZ,:DLYSZ,:DLYSCYZGZ,:GTJYBG,:INSPECTMAN,SYSDATE,:BZ,:PROBLEMS,:MEASURES,:SFAQGZ,:MHQ,:ORGCODE)";
            DataAccess.ExecuteNonQuery(strSQL, new OracleParameter(":ID", Guid.NewGuid().ToString()),
                new OracleParameter(":TRUCKNO", info.TRUCKNO),
                new OracleParameter(":DRIVERID", info.DRIVERID),
                new OracleParameter(":DRIVERNAME", info.DRIVERNAME),
                new OracleParameter(":PASSED", passed ? 1 : 0),
                new OracleParameter(":SFZ", chbID.Checked ? 1 : 0),
                new OracleParameter(":JSZ", chbJSZ.Checked ? 1 : 0),
                new OracleParameter(":XSZ", chbXSZ.Checked ? 1 : 0),
                new OracleParameter(":DLYSZ", chbDLYSZ.Checked ? 1 : 0),
                new OracleParameter(":DLYSCYZGZ", chbCYZGZ.Checked ? 1 : 0),
                new OracleParameter(":GTJYBG", chbGTJYBG.Checked ? 1 : 0),
                new OracleParameter(":INSPECTMAN", Session.CurrentUserName),
                new OracleParameter(":BZ", tbxBZ.Text.Trim()),
                new OracleParameter(":PROBLEMS", problems),
                new OracleParameter(":MEASURES", measures),
                new OracleParameter(":SFAQGZ", chbAQGZ.Checked ? 1 : 0),
                new OracleParameter(":MHQ", chbMHQ.Checked ? 1 : 0),
                new OracleParameter(":ORGCODE", Session.CurrentOrgCode));
        }

        private void AddToBlacklist(TruckInfo_Readonly info, string reason, string measures, bool needToDate, DateTime todate)
        {
            string strSQL = "insert into T_TRUCK_BLACKLIST(ID, TRUCKNO, DRIVERID, DRIVERNAME, REASON, MEASURE, ADDMAN, ADDTIME, ISDEL, ORGCODE, TODATE)"
                + " values(:ID, :TRUCKNO, :DRIVERID, :DRIVERNAME, :REASON, :MEASURE, :ADDMAN, :ADDTIME, :ISDEL, :ORGCODE, :TODATE)";
            DataAccess.ExecuteNonQuery(strSQL, new OracleParameter(":ID", Guid.NewGuid().ToString()),
                new OracleParameter(":TRUCKNO", info.TRUCKNO),
                new OracleParameter(":DRIVERID", info.DRIVERID),
                new OracleParameter(":DRIVERNAME", info.DRIVERNAME),
                new OracleParameter(":REASON", reason),
                new OracleParameter(":MEASURE", measures),
                new OracleParameter(":ADDMAN", Session.CurrentUserName),
                new OracleParameter(":ADDTIME", DateTime.Now),
                new OracleParameter(":ISDEL", "0"),
                new OracleParameter(":ORGCODE", Session.CurrentOrgCode),
                new OracleParameter(":TODATE", needToDate ? (object)todate : DBNull.Value));
        }

        private void Clear()
        {
            SetCardInfo(null);
            SetTruckInfo(null);
            this.chbAQGZ.Checked = false;
            this.txtWTGCS.Text = "0";
        }

        private void btnBeginRead_Click(object sender, EventArgs e)
        {
            BeginRead();
        }

        private void btnEndRead_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                reader.EndRead();
            }
            MessageBox.Show("已停止读身份证！");
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            Clear();
        }

        #region 增删改

        protected override void OnNew()
        {
            base.OnNew();
            using (CLGLForm form = new CLGLForm())
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    MessageBox.Show("新增车辆证件信息成功！");
                    TruckInfo_Readonly info = TruckInfo_Readonly.GetInstanceFromDB(form.ID);
                    TruckInfo_Readonly oldInfo = (TruckInfo_Readonly)this.propertyTruck.SelectedObject;
                    if (info != null)
                    {
                        info.DRIVERID = oldInfo.DRIVERID;
                        info.DRIVERNAME = oldInfo.DRIVERNAME;
                    }
                    SetTruckInfo(info);
                    //SetCardInfo(null);
                }
            }
        }

        protected override void OnModify()
        {
            base.OnModify();
            TruckInfo_Readonly info = (TruckInfo_Readonly)this.propertyTruck.SelectedObject;
            if (info == null || string.IsNullOrEmpty(info.ID))
                return;
            TruckCertInfo modifyInfo = TruckCertInfo.GetInstanceFromDB(info.ID);
            using (CLGLForm form = new CLGLForm(modifyInfo))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    MessageBox.Show("修改车辆证件信息成功！");
                    TruckInfo_Readonly newInfo = TruckInfo_Readonly.GetInstanceFromDB(form.ID);
                    newInfo.DRIVERID = info.DRIVERID;
                    newInfo.DRIVERNAME = info.DRIVERNAME;
                    SetTruckInfo(newInfo);
                    //SetCardInfo(null);
                }
            }
        }

        //protected override void OnDelete()
        //{
        //    base.OnDelete();
        //    TruckInfo_Readonly info = (TruckInfo_Readonly)this.propertyTruck.SelectedObject;
        //    if (info == null || string.IsNullOrEmpty(info.ID))
        //        return;
        //    if (MessageBox.Show("确定要删除当前数据吗?", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
        //    {
        //        DataAccess.ExecuteNonQuery("insert into T_TRUCK_INFO_LOG(ID,UPDATEMAN,UPDATEDATE,LX,TRUCKID,TRUCKNO,DRIVERID,DRIVERNAME,LOG,ORGCODE) " +
        //            "select :ID,:UPDATEMAN,Sysdate,2,ID,TRUCKNO,DRIVERID,DRIVERNAME,'删除',ORGCODE from T_TRUCK_INFO where ID=:TRUCKID",
        //            new OracleParameter(":ID", Guid.NewGuid().ToString()), new OracleParameter(":UPDATEMAN", Session.CurrentUserName), new OracleParameter(":TRUCKID", info.ID));
        //        int i = DataAccess.ExecuteNonQuery("delete from T_TRUCK_INFO where ID = '" + info.ID + "'");
        //        if (i > 0)
        //        {
        //            MessageBox.Show("删除成功！");
        //            Clear();
        //        }
        //    }
        //}

        #endregion

        private void contextMenuStripBlackHistory_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (cdgvBlackHistory.SelectedCells != null && cdgvBlackHistory.SelectedCells.Count > 0)
            {
                if (cdgvBlackHistory.SelectedCells[0].OwningRow.Cells["来源"].Value.ToString() == "黑名单")
                {
                    toolStripMenuItemDelete.Enabled = true;
                    return;
                }
            }
            toolStripMenuItemDelete.Enabled = false;
        }

        /// <summary>
        /// 移出黑名单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItemDelete_Click(object sender, EventArgs e)
        {
            if (cdgvBlackHistory.SelectedCells != null && cdgvBlackHistory.SelectedCells.Count > 0)
            {
                DataGridViewRow row = cdgvBlackHistory.SelectedCells[0].OwningRow;
                using (BlacklistDeleteForm frmDelete = new BlacklistDeleteForm(row.Cells["ID"].Value.ToString(), row.Cells["车牌号"].Value.ToString(),
                    row.Cells["司机身份证号"].Value.ToString(), row.Cells["司机姓名"].Value.ToString()))
                {
                    if (frmDelete.ShowDialog() == DialogResult.OK)
                    {
                        MessageBox.Show("移出黑名单成功！");
                        cdgvBlackHistory.Rows.Remove(row);
                    }
                }
            }
        }
    }
}
